﻿using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseInformation.Common
{
    public class CustomLog
    {
        public string UserName { get; set; } = "";


        public  string LogTemplate 
        {
            get {
                return "{Timestamp:yyyy-MM-dd HH:mm:ss} [" 
                    + $"{ UserName }" +"] [{Level:u3}] {Message:lj}{NewLine}{Exception}"; }
            set {; }
        
        }

        public  ILogger MyLog() 
        {
            return Log.Logger;
        }
  
        public void InitLog(string username ="") 
        {
            UserName = username;
            Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Information()
            .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
            .MinimumLevel.Override("System", LogEventLevel.Warning)
            .MinimumLevel.Override("Microsoft.AspNetCore.Authentication", LogEventLevel.Warning)
            .Enrich.FromLogContext()
            .WriteTo.Console(outputTemplate: LogTemplate).WriteTo.RollingFile("logs/login.txt", outputTemplate: LogTemplate)
            .CreateLogger();
        }



    }
}
